package com.suzhouhe.util.handler;

import com.suzhouhe.util.exception.ServiceException;
import com.suzhouhe.util.response.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * http异常捕捉类
 *
 * @author suzhouhe
 * @date 2020-11-03 16:10
 **/
@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 捕获http异常
     *
     * @param e 异常信息
     * @return http返回结果
     */
    @ExceptionHandler(value = {ServiceException.class})
    public <T> Result<T> httpErrorHandler(ServiceException e) {
        log.error("throw service exception,", e);
        return new Result<T>().failed(e.getMsg());
    }

    /**
     * 捕获异常
     *
     * @param e 异常信息
     * @return 返回结果
     */
    @ExceptionHandler(value = {Exception.class})
    public <T> Result<T> defaultException(Exception e) {
        log.error("throw other exception,", e);
        return new Result<T>().failed(e.getMessage());
    }
}
